package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.csshlj.service.ICsshljCzService;
import com.augurit.gzcgwsp.csshlj.web.form.CsshljCzForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IHwCsshljCzService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
/**
 * 解析 处置 xml数据   处置
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class HwCsshljCzServiceImpl implements IHwCsshljCzService {
	Logger logger = (Logger) LoggerFactory.getLogger(HwCsshljCzServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;
	
	@Autowired
	private ICsshljCzService csshljCzService;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String shljczfilepath = localResource.getString("shljczfilepath");

	// 附件存放路径
	private final String shljczfujianpath = localResource.getString("shljczfujianpath");

	// 解析成功反馈xml存放路径
	private final String shljczfeedbackPath = localResource.getString("shljczfeedbackPath");

	// 解析异常xml存放路径
	private final String shljczexceptionPath = localResource.getString("shljczexceptionPath");

	// 解析后，xml备份
	private final String shljczbackPath = localResource.getString("shljczbackPath");

	private Map<String, String> shljczKeyMap;// xml内容的key

	private Map<String, String> shljczKeyMapHead;// xml头部的key

	private Map<String, Long> shljczfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initshljczMap() {
		// 初始化数据部分对应的map
		shljczKeyMap = new HashMap<String, String>();
		shljczKeyMap.put("YSL_NO", "YSL_NO");
		shljczKeyMap.put("approvalNo", "approvalNo");
		shljczKeyMap.put("businessCode", "businessCode");
		shljczKeyMap.put("sendTime", "sendTime");
		
		shljczKeyMap.put("SQRQ", "SQRQ");
		shljczKeyMap.put("QYMC", "QYMC");
		shljczKeyMap.put("ZZJGDM", "ZZJGDM");
		shljczKeyMap.put("FRDB", "FRDB");
		shljczKeyMap.put("YYZZZCH", "YYZZZCH");
		shljczKeyMap.put("LXDH", "LXDH");
		shljczKeyMap.put("ZCZB", "ZCZB");
		shljczKeyMap.put("BGZS", "BGZS");//新增这个 - 2.20
		shljczKeyMap.put("XMMC", "XMMC");
		shljczKeyMap.put("CZ", "CZ");
		shljczKeyMap.put("EMAIL", "EMAIL"); 
		shljczKeyMap.put("YZBM", "YZBM");
		shljczKeyMap.put("WTSFJ", "WTSFJ");
		shljczKeyMap.put("DLLXDH", "DLLXDH");
		shljczKeyMap.put("DLRYDDH", "DLRYDDH");
		shljczKeyMap.put("DLRDZYX", "DLRDZYX");

		

		// 初始化头部对应的map
		shljczKeyMapHead = new HashMap<String, String>();
		shljczKeyMapHead.put("sn", "sn");
		shljczKeyMapHead.put("sender", "sender");
		// shljczKeyMapHead.put("receiver", "receiver");
		shljczKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		shljczfujianMap = new HashMap<String, Long>();
		shljczfujianMap.put("FJ_01", 362l);
		shljczfujianMap.put("FJ_02", 383l);
		shljczfujianMap.put("FJ_03", 369l);
		shljczfujianMap.put("FJ_04", 370l);
		shljczfujianMap.put("FJ_05", 371l);
		shljczfujianMap.put("FJ_06", 372l);
		shljczfujianMap.put("FJ_07", 373l);
		shljczfujianMap.put("FJ_08", 374l);
		shljczfujianMap.put("FJ_09", 375l);
		shljczfujianMap.put("FJ_10", 376l);
		shljczfujianMap.put("FJ_11", 377l);
		shljczfujianMap.put("FJ_12", 378l);
		shljczfujianMap.put("FJ_13", 380l);
		shljczfujianMap.put("FJ_14", 381l);
		shljczfujianMap.put("FJ_15", 382l);
		shljczfujianMap.put("FJ_16", 361l);
	}

	public void readXml(String projectPath) {
		if (shljczKeyMap == null)
			this.initshljczMap();
		File file = new File(shljczfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(shljczKeyMap, filePath,shljczfeedbackPath, shljczexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(shljczKeyMapHead,files[i], shljczfeedbackPath, shljczexceptionPath);

						// 把数据部分保存到数据库
						CsshljCzForm form = new CsshljCzForm();
						for (String mapKey : shljczKeyMap.keySet()) {							
							// TODO 把值放到form中
							form.setQymc(parseXMLService.getMapValue(shljczKeyMap.get("QYMC")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(shljczKeyMap.get("QYMC")),"shljcz");
							if(ywComQyxxForm != null){
								form.setQybh(ywComQyxxForm.getId().toString());
							}
							form.setZzjgdm(parseXMLService.getMapValue(shljczKeyMap.get("ZZJGDM")));
							form.setFrdb(parseXMLService.getMapValue(shljczKeyMap.get("FRDB")));
							form.setYyzzzch(parseXMLService.getMapValue(shljczKeyMap.get("YYZZZCH")));
							form.setLxdh(parseXMLService.getMapValue(shljczKeyMap.get("LXDH")));
							form.setZczb(parseXMLService.getMapValue(shljczKeyMap.get("ZCZB")));
//							form.setYzzm(parseXMLService.getMapValue(shljczKeyMap.get("YZZM")));//这个没有了 -2.20
							form.setBgzs(parseXMLService.getMapValue(shljczKeyMap.get("BGZS")));//新增这个  -2.20
							form.setXmmc(parseXMLService.getMapValue(shljczKeyMap.get("XMMC")));
							form.setCz(parseXMLService.getMapValue(shljczKeyMap.get("CZ")));
							form.setEmail(parseXMLService.getMapValue(shljczKeyMap.get("EMAIL")));
							form.setYzbm(parseXMLService.getMapValue(shljczKeyMap.get("YZBM")));
							form.setWtsfj(parseXMLService.getMapValue(shljczKeyMap.get("WTSFJ")));
							form.setDllxdh(parseXMLService.getMapValue(shljczKeyMap.get("DLLXDH")));
							form.setDlryddh(parseXMLService.getMapValue(shljczKeyMap.get("DLRYDDH")));
							form.setDlrdzyx(parseXMLService.getMapValue(shljczKeyMap.get("DLRDZYX")));
							
							
														
							
							String SQRQTemp = parseXMLService.getMapValue(shljczKeyMap.get("SQRQ"));
							Date SQRQ = new Date();
							if (org.apache.commons.lang.StringUtils.isNotEmpty(SQRQTemp)) {
								SQRQ = DateUtils.stringToDate(SQRQTemp,DateUtils.YYYY_MM_DD);
							}
							form.setSqrq(SQRQ);
							form.setCbyw(parseXMLService.getMapValue(shljczKeyMap.get("CBYW")));
							form.setCbbm(parseXMLService.getMapValue(shljczKeyMap.get("CBBM")));
							form.setYslNo(parseXMLService.getMapValue(shljczKeyMap.get("YSL_NO")));
							form.setApprovalNo(parseXMLService.getMapValue(shljczKeyMap.get("approvalNo")));							
							form.setBusinessCode(parseXMLService.getMapValue(shljczKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(shljczKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(
										wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setSendNotice("Y");
							form.setYsl(1l);
							
							form.setCbbm("环卫处");
							form.setCbyw("从事城市生活垃圾经营性处置服务许可审批");
							
						}
						
						// 调用DAO，保存到数据库
						try {
							csshljCzService.save(form);
//							csshljCzService.autoStartProc(form, "shljcz");
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									shljczKeyMapHead, shljczfeedbackPath, shljczexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, shljczfujianpath, shljczfeedbackPath, shljczexceptionPath, shljczKeyMapHead,
								files, shljczfujianMap, i, Integer.parseInt(form.getQybh()), form.getId(), "shljcz");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								shljczKeyMapHead, shljczfeedbackPath, shljczexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, shljczbackPath + fileName,
							fileName);
				}
			}
		}
	}

}
